<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Models\Role;
use App\Models\Node;

class RoleController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $sname = $request->get('search_name', '');
        //搜索用模型的when方法
        //规则：当参数1变量值存在，则执行参数2匿名函数,函数里头再加上sql条件。其实就是有条件判断地加sql条件
        //参数1，变量值存在，则执行匿名函数
        $data = Role::when($sname, function($query) use ($sname){
            $query->where('name', 'like', "%{$sname}%");
        })->paginate($this->item_num_per_page);


        return view('admin.role.index', compact('data', 'sname'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.role.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //接收 all接收所有数据
//        return $request->all();
        try {
            $post = $this->validate($request, [
                'name' => 'required|unique:roles,name'
            ]);
            //dd($request);
            Role::create($post);
            return ['status'=>0, 'msg'=>'角色添加成功'];
        }catch (\Exception $e) {
            return ['status'=>1000, 'msg'=>'验证不通过'];   //会在前端ajax的success回调作为参数
        }


    }


    public function show($id)
    {
        //
    }


    public function edit(int $id) {
        $model = Role::find($id);
        return view('admin.role.edit', compact('model'));
    }

    public function update(Request $request, int $id)
    {
        try {
            $post = $this->validate($request, [

                'name' => 'required|unique:roles,name,'. $id . ',id' //验证唯一性时忽略和自己id的比
            ]);
            //dd($request);

        }catch (\Exception $e) {
            return ['status'=>1000, 'msg'=>'验证不通过'];   //会在前端ajax的success回调作为参数
        }

//     也可以
//        $model = Role::find($id);
//        $model::update($post);
        Role::where('id', '=', $id)->update($post);
        return ['status'=>0, 'msg'=>'角色修改成功'];
    }

    public function destroy($id)
    {
        //
    }

    //给角色分配权限

    public function node(Role $role)
    {
        //读取出所有的权限
        $nodeAll = (new Node())->getAllList();
        //读取当前角色拥有的权限
        $nodes = $role->nodes()->pluck('id')->toArray();


        return view('admin.role.node', compact('role','nodeAll', 'nodes'));
    }

    //分配处理
    public function nodeSave(Request $request, Role $role) {
//        dump($request->all());
        //把上传的checkbox node数组作为要设置的Node表id同步到role_node表
        $role->nodes()->sync($request->get('node'));
//        return redirect(route('admin.role.node', $role));
        return redirect(route('admin.role.index'))->with(["success"=>"分配成功"]);
    }
}
